/**
 * Project:   warnme-app
 * File:      EventEntity.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      Mar 26, 2014
 */

package dtu.ds.warnme.model.impl;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import dtu.ds.warnme.model.AbstractEntity;
import dtu.ds.warnme.model.Mergeable;

/**
 * @author Bartosz Cichecki
 * 
 */
@Entity
@Table(name = "EVENTS")
public class EventEntity extends AbstractEntity implements Serializable, Mergeable<EventEntity> {

	@Transient
	private static final long serialVersionUID = 3319312715576707705L;

	@Column(name = "LATITUDE", nullable = false, unique = false)
	protected Float latitude;

	@Column(name = "LONGITUDE", nullable = false, unique = false)
	protected Float longitude;

	@Column(name = "BEARING", nullable = false, unique = false)
	protected Float bearing;

	@Enumerated(EnumType.STRING)
	@Column(name = "EVENT_TYPE", nullable = false, unique = false)
	protected EventType eventType;

	@Column(name = "REPORTS_COUNT", nullable = false, unique = false)
	protected Integer reportsCount;

	@Column(name = "NOTE", nullable = false, unique = false, length = 1000)
	protected String note;

	public EventEntity() {
		super();
	}

	public EventEntity(Float latitude, Float longitude, Float bearing, EventType eventType, Integer reportsCount, String note) {
		super();
		this.latitude = latitude;
		this.longitude = longitude;
		this.bearing = bearing;
		this.eventType = eventType;
		this.reportsCount = reportsCount;
		this.note = note;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		EventEntity other = (EventEntity) obj;
		if (bearing == null) {
			if (other.bearing != null) {
				return false;
			}
		} else if (!bearing.equals(other.bearing)) {
			return false;
		}
		if (eventType != other.eventType) {
			return false;
		}
		if (latitude == null) {
			if (other.latitude != null) {
				return false;
			}
		} else if (!latitude.equals(other.latitude)) {
			return false;
		}
		if (longitude == null) {
			if (other.longitude != null) {
				return false;
			}
		} else if (!longitude.equals(other.longitude)) {
			return false;
		}
		if (note == null) {
			if (other.note != null) {
				return false;
			}
		} else if (!note.equals(other.note)) {
			return false;
		}
		if (reportsCount == null) {
			if (other.reportsCount != null) {
				return false;
			}
		} else if (!reportsCount.equals(other.reportsCount)) {
			return false;
		}
		return true;
	}

	public Float getBearing() {
		return bearing;
	}

	public EventType getEventType() {
		return eventType;
	}

	public Float getLatitude() {
		return latitude;
	}

	public Float getLongitude() {
		return longitude;
	}

	public String getNote() {
		return note;
	}

	public Integer getReportsCount() {
		return reportsCount;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + (bearing == null ? 0 : bearing.hashCode());
		result = prime * result + (eventType == null ? 0 : eventType.hashCode());
		result = prime * result + (latitude == null ? 0 : latitude.hashCode());
		result = prime * result + (longitude == null ? 0 : longitude.hashCode());
		result = prime * result + (note == null ? 0 : note.hashCode());
		result = prime * result + (reportsCount == null ? 0 : reportsCount.hashCode());
		return result;
	}

	@Override
	public void merge(EventEntity entity) {
		setEventType(entity.getEventType());
		setLatitude(entity.getLatitude());
		setLongitude(entity.getLongitude());
		setNote(entity.getNote());
		setReportsCount(entity.getReportsCount());
	}

	/*
	 * (non-Javadoc)
	 * @see dtu.ds.warnme.model.AbstractEntity#prePersist()
	 */
	@Override
	public void prePersist() {
		super.prePersist();
		setReportsCount(0);
	}

	public void setBearing(Float bearing) {
		this.bearing = bearing;
	}

	public void setEventType(EventType eventType) {
		this.eventType = eventType;
	}

	public void setLatitude(Float latitude) {
		this.latitude = latitude;
	}

	public void setLongitude(Float longitude) {
		this.longitude = longitude;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public void setReportsCount(Integer reportsCount) {
		this.reportsCount = reportsCount;
	}

	@Override
	public String toString() {
		return "EventEntity [latitude=" + latitude + ", longitude=" + longitude + ", bearing=" + bearing + ", eventType=" + eventType
		        + ", reportsCount=" + reportsCount + ", note=" + note + "]";
	}

}
